package com.hg.library.http;

import android.os.Handler;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by ASUS-PC on 2016/6/14.
 */
public class TextRequestHandle extends RequestHandle {

    private Handler handler = new Handler();

    @Override
    public void onSuccess(Call call, Response response) {
        try {
            String content = response.body().string();
            handler.post(new Run(0,call, content));
        } catch (IOException e) {
            handler.post(new Run(1,call, e.getMessage()));
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(String error, Call call, IOException e) {
        handler.post(new Run(1,call, e.getMessage()));
    }

    public void onSuccess(Call call, String content) {
        /*empty*/
    }

    public void onFailure(Call call, String error) {
        /*empty*/
    }

    class Run implements Runnable {
        private int type;
        private String content;
        private Call call;

        public Run(int type, Call call, String content) {
            this.type = type;
            this.call = call;
            this.content = content;
        }

        @Override
        public void run() {
            if (type == 0)
                onSuccess(call, content);
            else
                onFailure(call,content);
        }
    }
}
